package org.song.videoplayer;


import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.*;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.global.configuration.Configuration;
import ohos.net.NetHandle;
import ohos.net.NetManager;
import ohos.wifi.WifiDevice;
import org.song.videoplayer.media.IMediaCallback;

import java.lang.reflect.Constructor;
import java.util.Optional;

/**
 * Created by song on 2017/2/13.
 * Contact github.com/tohodog
 * 工具类
 */

public class Util {
    //单位毫秒
    public static String stringForTime(int timeMs) {
        if (timeMs <= 0) {
            return "00:00";
        }
        int totalSeconds = timeMs / 1000;
        int seconds = totalSeconds % 60;
        int minutes = totalSeconds / 60;
        return String.format("%02d:%02d", minutes, seconds);
    }

    public static boolean isWifiConnected(Context context) {
        NetManager netManager = NetManager.getInstance(context);
//        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        NetHandle netHandle = netManager.getDefaultNet();
//        return networkInfo != null && networkInfo.getType() == netManager.TYPE_WIFI;
        // 获取WLAN管理对象
        WifiDevice wifiDevice = WifiDevice.getInstance(context);
        // 调用WLAN连接状态接口,确定当前设备是否连接WLAN
        boolean isConnected = wifiDevice.isConnected();
        return isConnected;
    }

    //保持常亮
    public static void KEEP_SCREEN_ON(Context context) {
        scanForActivity(context).getWindow().addFlags(WindowManager.LayoutConfig.MARK_SCREEN_ON_ALWAYS);
    }

    //取消常亮
    public static void KEEP_SCREEN_OFF(Context context) {
        scanForActivity(context).getWindow().clearFlags(WindowManager.LayoutConfig.MARK_SCREEN_ON_ALWAYS);
    }


    //全屏
    public static boolean SET_FULL(Context context) {
//        Window w = scanForActivity(context).getWindow();
        Optional<Window> topWindow = WindowManager.getInstance().getTopWindow();
        boolean b = false;
        if (topWindow.isPresent()) {
            Window w = topWindow.get();
//            DisplayAttributes displayAttributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
            b = (w.getLayoutConfig().get().flags & WindowManager.LayoutConfig.MARK_FULL_SCREEN) == WindowManager.LayoutConfig.MARK_FULL_SCREEN;
            w.addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        }

        //w.addFlags(WindowManage.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        //hideBottomUIMenu(context);
        //showNavigationBar(context, false);
        return b;
    }


    //取消全屏
    public static void CLEAR_FULL(Context context) {
//        Window w = scanForActivity(context).getWindow();
        Optional<Window> topWindow = WindowManager.getInstance().getTopWindow();
        if (topWindow.isPresent()) {
            Window w = topWindow.get();
            w.clearFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        }
        //w.clearFlags(WindowManage.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        //showBottomUIMenu(context);
        //showNavigationBar(context, true);
    }


    //横屏
    public static void SET_LANDSCAPE(Context context) {
        scanForActivity(context).setDisplayOrientation
                (AbilityInfo.DisplayOrientation.LANDSCAPE);
    }

    //竖屏
    public static void SET_PORTRAIT(Context context) {
        scanForActivity(context).setDisplayOrientation
                (AbilityInfo.DisplayOrientation.PORTRAIT);
    }

    //重力感应
    public static void SET_SENSOR(Context context) {
        scanForActivity(context).setDisplayOrientation
                (AbilityInfo.DisplayOrientation.UNSPECIFIED);
    }

    /**
     * 返回当前屏幕是否为竖屏。
     */
    public static boolean isScreenOriatationPortrait(Context context) {
        int i = context.getResourceManager().getConfiguration().direction;
        return i != Configuration.DIRECTION_HORIZONTAL;
    }


    /**
     * 隐藏虚拟按键
     */
    public static void hideBottomUIMenu(Context context) {
        showNavigationBar(context, false);
    }

    /**
     * 显示虚拟按键
     */
    public static void showBottomUIMenu(Context context) {
        showNavigationBar(context, true);
    }


    public static void showNavigationBar(Context context, boolean show) {
        try {
            Optional<Window> topWindow = WindowManager.getInstance().getTopWindow();
            if (show) {
                topWindow.ifPresent(window -> window.setStatusBarVisibility(Component.HIDE));
            } else {
                topWindow.ifPresent(window -> window.setStatusBarVisibility(Component.VISIBLE));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int dp2px(Context context, float value) {
//        final float scale = context.getResources().getDisplayMetrics().densityDpi;
        final float scale = context.getResourceManager().getDeviceCapability().screenDensity;

        return (int) (value * (scale / 160) + 0.5f);
    }


    public static AbilitySlice scanForActivity(Context context) {
        if (context instanceof AbilitySlice) {
            AbilitySlice a = (AbilitySlice) context;
            return a;
        } else if (context instanceof AbilityContext) {
            return scanForActivity(((AbilityContext) context).getContext());
        }
        throw new IllegalStateException("context得不到ability");
    }

    //url类型　0网络 -1本地 1直播流
    public static int PaserUrl(String url) {
        int mode = 0;
        if (url.startsWith("file"))
            mode = -1;
        if (url.endsWith("m3u8"))//...
            mode = 1;
        return mode;
    }

    /**
     * 实例化
     */
    public static <T extends Object> T newInstance(String className, IMediaCallback iMediaCallback) {
        Class<?>[] paramsTypes = new Class[]{IMediaCallback.class};
        try {
            Class<?> cls = Class.forName(className);
            Constructor<?> con = cls.getConstructor(paramsTypes);
            return (T) con.newInstance(new Object[]{iMediaCallback});
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
